############################################################################            
#                                 DATABASE                                 #
############################################################################

from google.appengine.ext import db
from google.appengine.ext.db import polymodel
from google.appengine.api import memcache



class Rooms(db.Model):
  
  def Query(cls):
    return db.Query(cls)
  Query = classmethod(Query)
  
  #Check if user is a member of that room     
  def isUserMember(self, userId):
    if userId == self.OwnerId : 
      return True  
    query = db.Query(Members)
    query.filter("Room", self)
    query.filter("MemberId", userId)
    if (query.count() > 0):
      return True
    else :
      return False
   
  Name    = db.StringProperty(multiline=False)
  OwnerId = db.StringProperty(multiline=False)
  

  
class Members(db.Model):
  
  def GetNick(cls, MemberId, RoomId):
    nickKey = MemberId+"_"+"%s" % RoomId
    nick = memcache.get(nickKey)
    if nick is None :
      query = Members.Query()
      query.filter('Room', RoomId)
      query.filter('MemberId', MemberId)
      member = query.get()
      nick = member.Nick
      memcache.add(nickKey, nick, 60)
    return nick
  GetNick = classmethod(GetNick)
  
  def Query(cls):
    return db.Query(cls)
  Query = classmethod(Query)
     
  Room     = db.ReferenceProperty(Rooms)
  MemberId = db.StringProperty(multiline=False)
  Nick     = db.StringProperty(multiline=False)



class Messages(polymodel.PolyModel):
  
  def GetMessagesForUser(cls, RoomsList, Limit, Offset):
    str = Limit, Offset
    gql = "SELECT * FROM Messages WHERE Room IN :1 ORDER BY TimeStamp DESC LIMIT %s OFFSET %s" % str                                       
    return db.GqlQuery(gql, RoomsList)
  GetMessagesForUser = classmethod(GetMessagesForUser)
  
  def Query(cls):
    return db.Query(cls)
  Query = classmethod(Query)
  
  def messageType(cls):
    return cls.class_name()
  
  Room      = db.ReferenceProperty(Rooms)
  TimeStamp = db.DateTimeProperty(auto_now_add=True)
  UserId    = db.StringProperty(multiline=False)
  
  
  
class RegularMessages(Messages):
  
  def Query(cls):
    return cls.all()
  Query = classmethod(Query)
  
  Message = db.TextProperty()
  
    
  
class Polls(Messages):
  
  def Query(cls):
    return cls.all()
  Query = classmethod(Query)
  
  #Check if the user already participated in the poll     
  def isUserParticipated(self, userId):
    if userId in self.Participaters :
      return True
    else :
      return False
  
  Question      = db.StringProperty(multiline=False)
  Answers       = db.ListProperty(str)
  Votes         = db.ListProperty(int)
  Participaters = db.ListProperty(str) 



class YouTube(Messages):
  
  def Query(cls):
    return cls.all()
  Query = classmethod(Query)
  
  Headline = db.StringProperty(multiline = False)
  Link     = db.StringProperty(multiline = False)   
 
  

class GadgetFiles(db.Model):
  
  def Query(cls):
    return cls.all()
  Query = classmethod(Query)
  
  Name      = db.StringProperty(multiline=False)
  TimeStamp = db.DateTimeProperty(auto_now_add=True,auto_now=True)
  Data      = db.TextProperty()


  
class FeedIndex(db.Model):
  
  def Add(cls, Message):
    query = Members.Query()
    members = query.filter('Room', Message.Room)
    for member in members :
      feedIndex = FeedIndex()  
      feedIndex.Room=Message.Room
      feedIndex.MemberId=member.MemberId
      feedIndex.Message=Message
      feedIndex.TimeStamp=Message.TimeStamp
      feedIndex.put()
    return

  def Remove(cls, Message):
    query = cls.Query()
    feedEntities = query.filter('Message', Message) 
    for feedEntity in feedEntities :
      feedEntity.delete()
    return  
        
  def Query(cls):
    return db.Query(cls)
  
  Add = classmethod(Add)
  Remove = classmethod(Remove)  
  Query = classmethod(Query)
  
  MemberId  = db.StringProperty(multiline=False)
  Room      = db.ReferenceProperty(Rooms)
  TimeStamp = db.DateTimeProperty(auto_now_add=False)
  Message   = db.ReferenceProperty(Messages)